package com.example.timmy.myapplication.ui.activity;

import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;

import com.example.timmy.myapplication.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

/**
 * MPAndroidChat 示例图
 * 刘桂林
 * http://blog.csdn.net/qq_26787115/article/details/53185657
 * http://blog.csdn.net/u014136472/article/details/50293767
 */
public class AndroidChatActivity1 extends AppCompatActivity {

    private static final String TAG = AndroidChatActivity1.class.getSimpleName();
    private LineChart mDoubleLineChar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_chat1);

        initChart();
    }

    private void initChart() {
        mDoubleLineChar = (LineChart) findViewById(R.id.mDoubleLineChar);

        //设置数值选择监听
        mDoubleLineChar.setOnChartValueSelectedListener(mOnChartValueSelectedListener);
        // 没有描述的文本
        mDoubleLineChar.getDescription().setEnabled(false);

        // 支持触控手势
        mDoubleLineChar.setTouchEnabled(true);
        mDoubleLineChar.setDragDecelerationFrictionCoef(0.9f);
        // 支持缩放和拖动
        mDoubleLineChar.setDragEnabled(true);
        mDoubleLineChar.setScaleEnabled(false);
        mDoubleLineChar.setDrawGridBackground(false);
        mDoubleLineChar.setHighlightPerDragEnabled(true);
        // 如果禁用,扩展可以在x轴和y轴分别完成
        mDoubleLineChar.setPinchZoom(true);
        // 设置背景颜色(灰色)
        // mDoubleLineChar.setBackgroundColor(Color.BLACK);

        Matrix m = new Matrix();
        // 第一个参数是方法缩小 X 轴的比例,第二个参数是放大缩小 Y 轴的比例
        m.postScale(1f, 1f);
        mDoubleLineChar.getViewPortHandler().refresh(m, mDoubleLineChar, false);

        //设置数据
        setData(3, 5);
        //默认x动画
        // mDoubleLineChar.animateX(2500);

        //获得数据
        Legend l = mDoubleLineChar.getLegend();

        //修改
        /**
         l.setForm(Legend.LegendForm.LINE);
         l.setTextSize(11f);
         l.setTextColor(Color.WHITE);
         l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
         l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
         l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
         l.setDrawInside(false);
         */
        l.setEnabled(false);

//        //x轴
//        XAxis xAxis = mDoubleLineChar.getXAxis();
//        xAxis.setTextSize(11f);
//        xAxis.setTextColor(Color.BLACK);
//        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置X轴的位置
//        xAxis.setDrawLabels(true); // 设置X轴的位置
//
//        // 是否绘制 X 轴上的竖线
//        xAxis.setDrawGridLines(false);
//        xAxis.setDrawAxisLine(false);

        //=======================设置X轴显示效果==================
        XAxis xAxis = mDoubleLineChar.getXAxis();
        //是否启用X轴
        xAxis.setEnabled(true);
        //是否绘制X轴线
        xAxis.setDrawAxisLine(true);
        xAxis.setAxisLineWidth(1.5f);
        //设置X轴上每个竖线是否显示
        xAxis.setDrawGridLines(false);
        //设置是否绘制X轴上的对应值(标签)
        xAxis.setDrawLabels(true);
        //设置X轴显示位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

        xAxis.setGranularity(1f);
        xAxis.setSpaceMax(7f);
        xAxis.setAxisMinimum(1);
        xAxis.setXOffset(5f);


        //左边y轴
        YAxis leftAxis = mDoubleLineChar.getAxisLeft();
        leftAxis.setXOffset(50);
        leftAxis.setTextColor(ColorTemplate.getHoloBlue());
        leftAxis.setAxisMaximum(200f);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setDrawGridLines(true);
        leftAxis.setGranularityEnabled(false);
        // 禁用
        leftAxis.setEnabled(false);

        // 设置x轴的LimitLine，index是从0开始的
        LimitLine yLimitLine1 = new LimitLine(160f);
        yLimitLine1.setLineColor(Color.RED);
        yLimitLine1.setTextColor(Color.GRAY);
        yLimitLine1.enableDashedLine(10f, 10f, 0f);
        leftAxis.addLimitLine(yLimitLine1);

        LimitLine yLimitLine2 = new LimitLine(80f);
        yLimitLine2.setLineColor(Color.RED);
        yLimitLine2.setTextColor(Color.GRAY);
        yLimitLine2.enableDashedLine(10f, 10f, 0f);
        leftAxis.addLimitLine(yLimitLine2);

        YAxis axisRight = mDoubleLineChar.getAxisRight();
        axisRight.setEnabled(false);
    }

    private void setData(int count, float range) {

        ArrayList<Entry> yVals1 = new ArrayList<>();
        for (int i = 1; i < count; i++) {
            float mult = range / 2f;
            float val = (float) (Math.random() * mult) + 150;
            yVals1.add(new Entry(i, val));
        }

        ArrayList<Entry> yVals2 = new ArrayList<Entry>();
        for (int i = 1; i < count; i++) {
            float mult = range;
            float val = (float) (Math.random() * mult) + 50;
            yVals2.add(new Entry(i, val));
        }


        LineDataSet set1, set2;

        if (mDoubleLineChar.getData() != null &&
                mDoubleLineChar.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) mDoubleLineChar.getData().getDataSetByIndex(0);
            set2 = (LineDataSet) mDoubleLineChar.getData().getDataSetByIndex(1);
            set1.setValues(yVals1);
            set2.setValues(yVals2);
            mDoubleLineChar.getData().notifyDataChanged();
            mDoubleLineChar.notifyDataSetChanged();
        } else {
            // 创建一个数据集,并给它一个类型
            set1 = new LineDataSet(yVals1, "");

            set1.setAxisDependency(YAxis.AxisDependency.LEFT);
            set1.isDrawValuesEnabled();
            set1.setColor(ColorTemplate.getHoloBlue());
            set1.setCircleColor(Color.WHITE);
            set1.setLineWidth(2f);
            set1.setCircleRadius(3f);
            set1.setFillAlpha(65);
            set1.setFillColor(ColorTemplate.getHoloBlue());
            set1.setHighLightColor(Color.rgb(244, 117, 117));
            set1.setDrawCircleHole(false);
            set1.setDrawFilled(true);

            //创建一个数据集,并给它一个类型
            set2 = new LineDataSet(yVals2, "");
            set1.isDrawValuesEnabled();
            set2.setAxisDependency(YAxis.AxisDependency.RIGHT);
            int LineColor2 = ContextCompat.getColor(this, R.color.line_color1);
            set2.setColor(LineColor2);

            set2.setDrawFilled(true);
            set2.setFillAlpha(65);
            set2.setFillColor(LineColor2);

            // 关闭 highlight 線
            set2.setDrawHighlightIndicators(false);
            // 设置垂直线
            set2.setDrawVerticalHighlightIndicator(true);
            set2.setHighLightColor(Color.rgb(244, 117, 117));

            set2.setHighlightEnabled(true);
            set2.setDrawValues(false);
            set2.setLineWidth(1.5f);

            set2.setCircleColor(LineColor2);
            // 小圆点的大小
            set2.setCircleRadius(6f);
            // 里面白色小圆的大小
            set2.setCircleHoleRadius(4f);
            // set2.setDrawCircleHole(false);

            // 创建一个数据集的数据对象
            LineData data = new LineData(set1, set2);
            data.setValueTextColor(Color.WHITE);
            data.setValueTextSize(9f);

            //设置数据
            mDoubleLineChar.setData(data);
        }
    }

    // ========================== Listener Start =================================
    private OnChartGestureListener mOnChartGestureListener = new OnChartGestureListener() {
        @Override
        public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
            Log.i(TAG, "onChartGestureStart");
        }

        @Override
        public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
            Log.i(TAG, "onChartGestureEnd");
        }

        @Override
        public void onChartLongPressed(MotionEvent me) {
            Log.i(TAG, "onChartLongPressed");
        }

        @Override
        public void onChartDoubleTapped(MotionEvent me) {
            Log.i(TAG, "onChartDoubleTapped");
        }

        @Override
        public void onChartSingleTapped(MotionEvent me) {
            Log.i(TAG, "onChartSingleTapped");
        }

        @Override
        public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
            Log.i(TAG, "onChartFling");
        }

        @Override
        public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
            Log.i(TAG, "onChartScale");
        }

        @Override
        public void onChartTranslate(MotionEvent me, float dX, float dY) {
            Log.i(TAG, "onChartTranslate");
        }
    };

    private OnChartValueSelectedListener mOnChartValueSelectedListener = new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry e, Highlight h) {
            Log.i(TAG, "onValueSelected");
        }

        @Override
        public void onNothingSelected() {
            Log.i(TAG, "onNothingSelected");

        }
    };

    // ========================== Listener End =================================
}
